/**
 *
 */
package com.naiterui.ehp.bs.doctor.dao.impl;

import cn.hutool.core.collection.CollectionUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.naiterui.ehp.bp.domain.DrPatientGroupDetail;
import com.naiterui.ehp.bp.domain.PatientGroup;
import com.naiterui.ehp.bs.doctor.dao.IPatientGroupDao;
import com.naiterui.ehp.bs.doctor.repository.IDrPatientGroupDetailRepository;
import com.naiterui.ehp.bs.doctor.repository.IPatientGroupRepository;

@Repository
public class AdPatientGroupDaoImpl implements IPatientGroupDao {

    @Autowired
    private IPatientGroupRepository patientGroupRepository;

    @Autowired
    private IDrPatientGroupDetailRepository drPatientGroupDetailRepository;

    @Override
    public void save(PatientGroup patientGroup) {
        this.patientGroupRepository.save(patientGroup);
    }

    @Override
    public PatientGroup get(Long id) {
        return this.patientGroupRepository.get(id);
    }

    @Override
    public void saveOrUpdate(PatientGroup patientGroup) {
        if (patientGroup.getId() == null) {
            this.patientGroupRepository.save(patientGroup);
        } else {
            this.patientGroupRepository.update(patientGroup);
        }
    }

    @Override
    public void removeById(Long id) {
        this.patientGroupRepository.removeById(id);
    }

    @Override
    public List<PatientGroup> getPatientGroupList(Long doctorId) {
        return this.patientGroupRepository.findByDoctorIdOrderByCreatedAtDesc(doctorId);
    }

    @Override
    public boolean findExistNameByDoctorId(Long doctorId, String groupName, Long groupId) {
        long count = this.patientGroupRepository.countByDoctorIdAndName(doctorId, groupName);
        return count > 0;
    }

    /**
     * 批量查询患者分组名称
     * @Author YongYang
     * @Date 2017年3月2日
     * @since 1.0.0
     * @return
     */
    @Override
    public List<String> findPatientGroupNameList(String groupIds) {
        List<Long> ids = Arrays.asList(StringUtils.split(groupIds, ",")).stream().map(Long::parseLong).collect(Collectors.toList());
        List<PatientGroup> list = this.patientGroupRepository.findByIdInOrderByCreatedAtDesc(ids);
        return list.stream().map(PatientGroup::getName).collect(Collectors.toList());
    }

    @Override
    public List<PatientGroup> getGroupListByPatient(Long doctorId, Long patientId) {
        List<PatientGroup> groupList = new ArrayList<>();
        List<DrPatientGroupDetail> detialList = this.drPatientGroupDetailRepository.findByDoctorIdAndPatientId(doctorId, patientId);
        if (CollectionUtil.isNotEmpty(detialList)) {
            List<Long> groupIds = detialList.stream().map(DrPatientGroupDetail::getGroupId).collect(Collectors.toList());
            groupList = this.patientGroupRepository.findByIdInOrderByCreatedAtDesc(groupIds);
        }

        return groupList;
    }

    @Override
    public long getGroupsCount(Set<Long> groupIds) {
        return this.patientGroupRepository.countByIdIn(groupIds);
    }
}
